package com.topdt.jbpm.web.servlet.action;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jbpm.api.model.ActivityCoordinates;

import com.topdt.jbpm.identity.Department;
import com.topdt.jbpm.identity.IdFactory;
import com.topdt.jbpm.identity.IdentityService;
import com.topdt.jbpm.identity.User;
import com.topdt.jbpm.model.HistTodo;
import com.topdt.jbpm.model.Todo;
import com.topdt.jbpm.model.WorkOpinion;
import com.topdt.jbpm.web.servlet.JbpmServlet;
import com.topdt.jbpm.web.servlet.view.HistTodoView;
import com.topdt.jbpm.web.servlet.view.HistTodoViewComparator;
/**
 * 流程跟踪
 * 建立日期 : 2016年11月24日 下午5:53:45<br>
 * 作者 : zhangyong<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
public class FlowLogAction extends JbpmBaseAction {

	public FlowLogAction(JbpmServlet servlet) {
		super(servlet);
	}

	
	
	/**
	 * 查找流转日志
	 * @author zhangyong
	 * @date   2016年11月28日 下午4:26:53 
	 * @param request
	 * @param response
	 */
	public void findWorkRecord(HttpServletRequest request, HttpServletResponse response){
		List<HistTodoView> listView = new ArrayList<HistTodoView>();
		try {
			String workId = request.getParameter("workId");
			//历史待办
			List<HistTodo> listHistTodo = jbpmService.findWorkRecordFromHistTodo(workId);
			//待办
			List<Todo> listTodo = jbpmService.findWorkRecordFromTodo(workId);
			IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
			if (listHistTodo != null && listHistTodo.size() > 0) {
				for (int i = 0; i < listHistTodo.size(); i++) {
					HistTodoView histTodoView = new HistTodoView();
					histTodoView.setActivityName(listHistTodo.get(i).getActivityName());
					if (listHistTodo.get(i).getDestActivityName() == null
							|| "".equals(listHistTodo.get(i).getDestActivityName())) {
						histTodoView.setDestActivityName("");
					} else {
						histTodoView.setDestActivityName(listHistTodo.get(i).getDestActivityName());
					}
					histTodoView.setArriveTime(listHistTodo.get(i).getArriveTime());
					histTodoView.setFinishTime(listHistTodo.get(i).getFinishTime());
					User user = idService.getUserByUserId(listHistTodo.get(i).getToUser());
					Department department = idService.getMajorDepOfUser(user.getId());
					if (department.getName() == null || "".equals(department.getName())) {
						histTodoView.setDepName("");
					} else {
						histTodoView.setDepName(department.getName());
					}
					histTodoView.setState(listHistTodo.get(i).getState());
					histTodoView.setUserName(user.getName());
					histTodoView.setBatch((listHistTodo.get(i).getBatch()));
					//设置意见
					List<WorkOpinion> opinions = jbpmService.findOpinionByWorkIdAndTodoId(workId, listHistTodo.get(i).getTodoId());
					if(opinions != null && opinions.size()>0 ) {
						histTodoView.setOpinion(opinions.get(0).getOpinionContent());
					} else {
						histTodoView.setOpinion("");
					}

					listView.add(histTodoView);
				}
			}
			if (listTodo != null && listTodo.size() > 0) {
				for (Todo histTodo : listTodo) {
					HistTodoView histTodoView = new HistTodoView();
					histTodoView.setActivityName(histTodo.getActivityName());
					histTodoView.setArriveTime(histTodo.getArriveTime());
					if (histTodo.getDestActivityName() == null || "".equals(histTodo.getDestActivityName())) {
						histTodoView.setDestActivityName("");
					} else {
						histTodoView.setDestActivityName(histTodo.getDestActivityName());
					}
					User user = idService.getUserByUserId(histTodo.getToUserId());
					Department department = idService.getMajorDepOfUser(user.getId());
					if (department.getName() == null || "".equals(department.getName())) {
						histTodoView.setDepName("");
					} else {
						histTodoView.setDepName(department.getName());
					}
					histTodoView.setState(histTodo.getState());
					histTodoView.setUserName(user.getName());
					histTodoView.setBatch(histTodo.getBatch());
					//设置意见
					List<WorkOpinion> opinions = jbpmService.findOpinionByWorkIdAndTodoId(workId, histTodo.getTodoId());
					if(opinions != null && opinions.size()>0 ) {
						histTodoView.setOpinion(opinions.get(0).getOpinionContent());
					} else {
						histTodoView.setOpinion("");
					}

					listView.add(histTodoView);
				}
			}
			Collections.sort(listView, new HistTodoViewComparator());
			
		} catch (Exception e) {
			logger.error("<错误>: 查询流转日志异常", e);
		}			
		responseJson(response, listView);
	}
	/**
	 * 打印图片
	 *
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void printImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String workId = request.getParameter("workId");
		InputStream is = jbpmService.getFlowImageIs(workId);
		byte[] pic = new byte[1024];
		int len = -1;
		while ((len = is.read(pic, 0, 1024)) != -1) {
			response.getOutputStream().write(pic, 0, len);
		}
	}	
	/**
	 * 获得当前的活跃环节
	 *
	 */
	public void getActivityNate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String workId = request.getParameter("workId");
		List<ActivityCoordinates> acs = jbpmService.getActivityCoordinates(workId);
		responseJson(response, acs);
	}	
}